<?
require_once $_SERVER['DOCUMENT_ROOT'] . "/kernel/starter.php";
Module::Init('system:structure', 'system:jscore');
Module::Init('system:jscore');
Jscore::GetBridge();
Jscore::Engage('fe', JSLIBS . "/formeditor/formeditor.js", JSLIBS . "/formeditor/formeditor.css");
Jscore::Engage('jquery', JSLIBS . "/jquery/jquery.min.js");
Jscore::Engage('jqueryui', JSLIBS . "/jqueryui/jquery-ui.js", JSLIBS . "/jqueryui/css/base/jquery-ui.css");
Jscore::Init('jquery', 'jqueryui', 'fe');
BDS::Init();
if (check_action('delete_structure', 'post')) {
    $result = BDSStructures::Delete($_POST['structure_id']);
    if ($result) {
        Logger::ok(t("Section deleted. Close this window"));
    } else {
        Logger::error(t("Something wrong!"));
    }
}
if (check_action('delete_type', 'post')) {
    $result = BDSTypes::Delete($_POST['id']);
    if ($result) {
        Logger::ok(t("Type deleted. Close this window"));
    } else {
        Logger::error(t("Something wrong!"));
    }
}
if (check_action('delete_block', 'post')) {
    $result = BDSBlocks::Delete($_POST['id']);
    if ($result) {
        Logger::ok(t("Block deleted. Close this window"));
    } else {
        Logger::error(t("Something wrong!"));
    }
}
if (check_action('new_block', 'post')) {
    unset($_POST['action']);
    if (preg_match("#[\w\d]{1,32}#", $_POST['code']) && preg_match("#.{1,255}#", $_POST['title'])) {
        $result = BDSBlocks::Create($_POST);
    } else {
        Logger::error(t('Invlid data. Check fields'));
    }

    if ($result) {
        Logger::ok(t("Block created successful. You can create another new block in this section"));
    } else {
        Logger::error(t("Something wrong! Block create failed"));
    }
}
if (check_action('new_structure', 'post')) {
    unset($_POST['action']);
    if (preg_match("#[\w\d]{1,32}#", $_POST['code']) && preg_match("#.{1,255}#", $_POST['title'])) {
        $result = BDSStructures::Create($_POST);
    } else {
        Logger::error(t('Invlid data. Check fields'));
    }

    if ($result) {
        Logger::ok(t("Structure created successful. You can create another new structure in this section"));
    } else {
        Logger::error(t("Something wrong! Structure create failed"));
    }
}
if (check_action('new_type', 'post')) {
    unset($_POST['action']);
    if (preg_match("#[\w\d]{1,32}#", $_POST['code']) && preg_match("#.{1,255}#", $_POST['title'])) {
        $result = BDSTypes::Create($_POST);
    } else {
        Logger::error(t('Invlid data. Check fields'));
    }

    if ($result) {
        Logger::ok(t("Type created successful. You can create another new structure in this section"));
    } else {
        Logger::error(t("Something wrong! Type create failed"));
    }
}
if (check_action('edit_structure', 'post')) {
    $id = $_POST['id'];
    unset($_POST['action'], $_POST['id']);

    if (preg_match("#[\w\d]{1,32}#", $_POST['code']) && preg_match("#.{1,255}#", $_POST['title'])) {
        $result = BDSStructures::Update($id, $_POST);
    } else {
        Logger::error(t('Invlid data. Check fields'));
    }

    if ($result) {
        Logger::ok(t("Structure update successful"));
    } else {
        Logger::error(t("Something wrong! Structure update failed"));
    }
}
if (check_action('edit_type', 'post')) {
    $id = $_POST['id'];
    unset($_POST['action'], $_POST['id']);

    if (preg_match("#[\w\d]{1,32}#", $_POST['code']) && preg_match("#.{1,255}#", $_POST['title'])) {
        $result = BDSTypes::Update($id, $_POST);
    } else {
        Logger::error(t('Invlid data. Check fields'));
    }

    if ($result) {
        Logger::ok(t("Type update successful"));
    } else {
        Logger::error(t("Something wrong! Type update failed"));
    }
}
if (check_action('edit_block', 'post')) {
    $id = $_POST['id'];
    unset($_POST['action'], $_POST['id']);

    if (preg_match("#[\w\d]{1,32}#", $_POST['code']) && preg_match("#.{1,255}#", $_POST['title'])) {
        $result = BDSBlocks::Update($id, $_POST);
    } else {
        Logger::error(t('Invlid data. Check fields'));
    }

    if ($result) {
        Logger::ok(t("Block update successful"));
    } else {
        Logger::error(t("Something wrong! Block update failed"));
    }
}
Logger::ShowAll();
?>

<? if (check_action('new_structure', 'get')): ?>
    <form method="post">
        <? set_action('new_structure') ?>
        <input type="hidden" name="type_id" value="<?= $_REQUEST['tid'] ?>">
        <div class="input">
            <label for="code"><?= t('Structure code'); ?><span class="required">*</span></label><input type="text" name="code" value="">
        </div>
        <div class="input">
            <label for="code"><?= t('Structure title'); ?><span class="required">*</span></label><input type="text" name="title" value="">
        </div>
        <div class="input">
            <input type="submit" value="<?= t('Create new structure'); ?>">
        </div>
    </form>
<? endif; ?>

<? if (check_action('new_type', 'get')): ?>
    <form method="post">
        <? set_action('new_type') ?>
        <input type="hidden" name="site_id" value="<?= $_REQUEST['sid'] ?>">
        <div class="input">
            <label for="code"><?= t('Type code'); ?><span class="required">*</span></label><input type="text" name="code" value="">
        </div>
        <div class="input">
            <label for="code"><?= t('Type title'); ?><span class="required">*</span></label><input type="text" name="title" value="">
        </div>
        <div class="input">
            <input type="submit" value="<?= t('Create new type'); ?>">
        </div>
    </form>
<? endif; ?>

<? if (check_action('edit_structure', 'get')): ?>
    <form method="post">
        <? set_action('edit_structure') ?>
        <? $struct = BDSStructures::GetById($_REQUEST['sid']); ?>
        <input type="hidden" name="id" value="<?= $_REQUEST['sid'] ?>">
        <div class="input">
            <label for="code"><?= t('Structure code'); ?><span class="required">*</span></label><input type="text" name="code" value="<?= $struct['code'] ?>">
        </div>
        <div class="input">
            <label for="code"><?= t('Structure title'); ?><span class="required">*</span></label><input type="text" name="title" value="<?= $struct['title'] ?>">
        </div>
        <div class="input">
            <input type="submit" value="<?= t('Save'); ?>">
        </div>
    </form>
<? endif; ?>

<? if (check_action('edit_type', 'get')): ?>
    <form method="post">
        <? set_action('edit_type') ?>
        <? $struct = BDSTypes::GetById($_REQUEST['tid']); ?>
        <input type="hidden" name="id" value="<?= $_REQUEST['tid'] ?>">
        <div class="input">
            <label for="code"><?= t('Type code'); ?><span class="required">*</span></label><input type="text" name="code" value="<?= $struct['code'] ?>">
        </div>
        <div class="input">
            <label for="code"><?= t('Type title'); ?><span class="required">*</span></label><input type="text" name="title" value="<?= $struct['title'] ?>">
        </div>
        <div class="input">
            <input type="submit" value="<?= t('Save'); ?>">
        </div>
    </form>
<? endif; ?>

<? if (check_action('edit_block', 'get')): ?>
    <form method="post">
        <? set_action('edit_block') ?>
        <? $struct = BDSBlocks::GetById($_REQUEST['bid']); ?>
        <input type="hidden" name="id" value="<?= $_REQUEST['bid'] ?>">
        <div class="input">
            <label for="code"><?= t('Block code'); ?><span class="required">*</span></label><input type="text" name="code" value="<?= $struct['code'] ?>">
        </div>
        <div class="input">
            <label for="code"><?= t('Block title'); ?><span class="required">*</span></label><input type="text" name="title" value="<?= $struct['title'] ?>">
        </div>
        <div class="input">
            <input type="submit" value="<?= t('Save'); ?>">
        </div>       
    </form>
<? endif; ?>

<? if (check_action('new_block', 'get')): ?>
    <form method="post">
        <? set_action('new_block') ?>
        <input type="hidden" name="structure_id" value="<?= $_REQUEST['sid'] ?>">
        <div class="input">
            <label for="code"><?= t('Block code'); ?><span class="required">*</span></label><input type="text" name="code" value="">
        </div>
        <div class="input">
            <label for="code"><?= t('Block title'); ?><span class="required">*</span></label><input type="text" name="title" value="">
        </div>
        <div class="input">
            <input type="submit" value="<?= t('Create new block'); ?>">
        </div>        
    </form>
<? endif; ?>

<? if (check_action('section_fields', 'get')): ?>
    <?php
    
    $toolset = BDSFields::GetType();
    Jscore::PhpToJs('toolset', $toolset);
    $form = BDSFields::GetList(array('block_id' => $_GET['bid'], "relation" => 'S'));
    Jscore::PhpToJs('formsettings', array('block_id' => $_GET['bid'], "relation" => 'S'));
    Jscore::PhpToJs('form', $form);
    ?>
    <div class="formeditor"></div>
<? endif; ?>

<? if (check_action('element_fields', 'get')): ?>
    <?php
    $toolset = BDSFields::GetType();
    Jscore::PhpToJs('toolset', $toolset);
    $blocks = array(BDSBlocks::GetById($_GET['bid']));
    $form = BDSFields::GetList(array('block_id' => $_GET['bid'], "relation"=>"E"));
    Jscore::PhpToJs('formsettings', array('block_id' => $_GET['bid'], "relation" => 'E'));
    Jscore::PhpToJs('form', $form);
    ?>
    <div class="formeditor"></div>
<? endif; ?>

<? if (!check_action('delete_structure', 'post') && check_action('delete_structure', 'get')): ?>
    <form method="post">
        <? set_action('delete_structure'); ?>
        <input type="hidden" name="structure_id" value="<?= $_GET['sid'] ?>">
        <input type="submit" value="<?= t('Are you shure?'); ?>">
        </div>
    </form>
<? endif; ?>

<? if (!check_action('delete_type', 'post') && check_action('delete_type', 'get')): ?>
    <form method="post">
        <? set_action('delete_type'); ?>
        <input type="hidden" name="id" value="<?= $_GET['tid'] ?>">
        <input type="submit" value="<?= t('Are you shure?'); ?>">
        </div>
    </form>
<? endif; ?>

<? if (!check_action('delete_block', 'post') && check_action('delete_block', 'get')): ?>
    <form method="post">
        <? set_action('delete_block'); ?>
        <input type="hidden" name="id" value="<?= $_GET['bid'] ?>">
        <input type="submit" value="<?= t('Are you shure?'); ?>">
        </div>
    </form>
<? endif; ?>
